package cn.iocoder.yudao.module.mbti.controller.admin.mbti;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.mbti.controller.admin.mbti.vo.*;
import cn.iocoder.yudao.module.mbti.convert.mbti.MbtiConvert;
import cn.iocoder.yudao.module.mbti.dal.dataobject.mbti.MbtiDO;
import cn.iocoder.yudao.module.mbti.service.mbti.MbtiService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

@Tag(name = "管理后台 - mbti类型")
@RestController
@RequestMapping("/mbti")
@Validated
public class MbtiController {

    @Resource
    private MbtiService Service;

    @GetMapping("/page2")
    @Operation(summary = "获得动态表单分页")
    public CommonResult<PageResult<MbtiRespVO>> getFormPage(@Valid MbtiPageReqVO pageVO) {
        return null;
    }

    @PostMapping("/create")
    @Operation(summary = "创建mbti类型")
    @PreAuthorize("@ss.hasPermission('mbti:create')")
    public CommonResult<Long> create(@Valid @RequestBody MbtiCreateReqVO createReqVO) {
        return success(Service.create(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新mbti类型")
    @PreAuthorize("@ss.hasPermission('mbti:update')")
    public CommonResult<Boolean> update(@Valid @RequestBody MbtiUpdateReqVO updateReqVO) {
        Service.update(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除mbti类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mbti:delete')")
    public CommonResult<Boolean> delete(@RequestParam("id") Long id) {
        Service.delete(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得mbti类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mbti:query')")
    public CommonResult<MbtiRespVO> get(@RequestParam("id") Long id) {
        MbtiDO mbtiDO = Service.get(id);
        return success(MbtiConvert.INSTANCE.convert(mbtiDO));
    }

    @GetMapping("/list")
    @Operation(summary = "获得mbti类型列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mbti:query')")
    public CommonResult<List<MbtiRespVO>> getList(@RequestParam("ids") Collection<Long> ids) {
        List<MbtiDO> list = Service.getList(ids);
        return success(MbtiConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得mbti类型分页")
    @PreAuthorize("@ss.hasPermission('mbti:query')")
    public CommonResult<PageResult<MbtiRespVO>> getPage(@Valid MbtiPageReqVO pageVO) {
        PageResult<MbtiDO> pageResult = Service.getPage(pageVO);
        return success(MbtiConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出mbti类型 Excel")
    @PreAuthorize("@ss.hasPermission('mbti:export')")
    @OperateLog(type = EXPORT)
    public void exportExcel(@Valid MbtiExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<MbtiDO> list = Service.getList(exportReqVO);
        // 导出 Excel
        List<MbtiExcelVO> datas = MbtiConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "mbti类型.xls", "数据", MbtiExcelVO.class, datas);
    }

}
